#include "gtest/gtest.h"


class IBitCount {
public:
    virtual ~IBitCount(){}
    virtual int run(unsigned int n) = 0;
};

class BitCount : public IBitCount{
public:
    ~BitCount()override{}

    int run(unsigned int n) override{
        int c = 0;
        for(; n != 0; n >>= 1){
            if(n & 1) 
                ++c; 
        }
        return c;
    }
};

class BitCountQuick : public IBitCount{
    ~BitCountQuick()override{}

    int run(unsigned int n) override{
        unsigned int i = 0;
        for(; n != 0; ++i){
            n &= n-1;
        }
        return (int)i;
    }
};


#include "helper/ScopedMemPtr.h"
TEST(BitCountTest, test){
    helper::ScopedMemPtr<IBitCount> bc = new BitCount;

    ASSERT_EQ(0, bc->run(0u));
    ASSERT_EQ(1, bc->run(1u));
    ASSERT_EQ(6, bc->run(0x10101111u));
    ASSERT_EQ(32, bc->run(0x0-1u));
}
TEST(BitCountTest, testQuick){
    helper::ScopedMemPtr<IBitCount> bc = new BitCountQuick;
    ASSERT_EQ(0, bc->run(0u));
    ASSERT_EQ(1, bc->run(1u));
    ASSERT_EQ(6, bc->run(0x10101111u));
    ASSERT_EQ(32, bc->run(0x0-1u));

}
